import pymysql
from pyecharts.charts import Bar
from pyecharts import options as opts

db = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='1234',
    database='crawlerdemo',
    charset='utf8',
)
cursor = db.cursor()
sql = "SELECT min(job_sal) AS min_sal, job_place FROM job GROUP BY job_place ORDER BY min_sal ASC"
sal = []
city = []
try:
    cursor.execute(sql)
    res = cursor.fetchall()
    for i in res:
        sal.append(i[0])
        city.append(i[1])
    bar = Bar()
    bar.add_xaxis(city)
    bar.add_yaxis(
        "月薪水/元",
        sal,
        label_opts=opts.LabelOpts(is_show=True, formatter="{c} 元")  # 显示数值标签
    )
    bar.set_global_opts(
        title_opts=opts.TitleOpts(title="各城市最低薪资", subtitle="单位/元"),
        # 还可以添加其他全局配置，比如坐标轴的配置
    )
    bar.render("barmin.html")
except :
    print("查询出问题了")

# 最后记得关闭数据库连接
db.close()